package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.ProductInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Ching on 2016/4/6.
 * 进件业务产品Adapter
 */
public class IntoProAdapter extends BaseAdapter {

    private boolean isFirst = true;

    // 用于记录每个RadioButton的状态，并保证只可选一个
    HashMap<String, Boolean> states = new HashMap<String, Boolean>();

    private List<ProductInfo> list = new ArrayList<>();

    private Context mContext;
    ProductInfo proInfo = null;


    public IntoProAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void addAll(List<ProductInfo> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void removeAll() {
        this.list.clear();
        notifyDataSetChanged();
    }

    public void setList(List<ProductInfo> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public  List<ProductInfo>  getDatas() {
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_into_product_list, parent, false);
            holder = new ViewHolder();
            holder.proName = (TextView) convertView.findViewById(R.id.tv_into_proName);
            holder.explain = (TextView) convertView.findViewById(R.id.tv_into_explain);
            convertView.setTag(holder);
        }

        holder.proName.setText(list.get(position).getProName());
        holder.explain.setText(list.get(position).getExplain());


        final RadioButton rbtn = (RadioButton) convertView.findViewById(R.id.rbtn_into_product);
        holder.rbtn = rbtn;
        if (isFirst && position == 0) {//默认第一个被选中
            isFirst = false;
            holder.rbtn.setChecked(true);
            states.put(String.valueOf(position), rbtn.isChecked());
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 重置，确保最多只有一项被选中
                for (String key : states.keySet()) {
                    states.put(key, false);
                }
                states.put(String.valueOf(position), true);
                IntoProAdapter.this.notifyDataSetChanged();
            }
        });
//        holder.rbtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        boolean flag = false;
        if (states.get(String.valueOf(position)) == null || states.get(String.valueOf(position)) == false) {
            flag = false;
            states.put(String.valueOf(position), false);
        } else {
            flag = true;
            proInfo = list.get(position);
        }
        holder.rbtn.setChecked(flag);

        return convertView;
    }

    class ViewHolder {

        public TextView proName;
        public TextView explain;
        public RadioButton rbtn;
    }

    public final ProductInfo getProInfo() {
        return proInfo;
    }

}
